<template>
  <div class="wrapper">
    <div class="header">
      <div class="header__info">
        <div class="header__info__user">{{ userInfo.username }}</div>
        <div class="header__info__id">ID:{{ userInfo._id }}</div>
      </div>
      <img
        class="header__avatar"
        src="https://inews.gtimg.com/newsapp_bt/0/13386674055/1000"
      />
    </div>
    <div class="list">
      <div class="list__item" @click="handleAddressClick">
        <div class="list__item__icon iconfont">&#xe62e;</div>
        <div class="list__item__test">我的地址</div>
        <div class="list__item__arrow iconfont">&#xe6db;</div>
      </div>
    </div>
    <div class="logout" @click="handleLogout">退出登陆</div>
  </div>
  <DocKer :currentIndex="3" />
</template>

<script>
import DocKer from "@/components/DocKer.vue";
import { useRouter } from "vue-router";
import { reactive } from "vue";
import { get } from "@/utils/request.js";

//处理'我的'相关逻辑
const useUserEffect = () => {
  const userInfo = reactive({ _id: "", username: "" });
  const getUserInfo = async () => {
    const result = await get("/api/user/info");
    if (result?.errno === 0 && result?.data) {
      const { _id, username } = result.data;
      userInfo._id = _id || "";
      userInfo.username = username || "";
    }
  };
  getUserInfo();
  return userInfo;
};

export default {
  name: "PersonalInfo",
  components: { DocKer },
  setup() {
    const router = useRouter();
    const handleLogout = () => {
      localStorage.removeItem("isLoGin");
      router.replace({ name: "LoGin" });
    };
    const handleAddressClick = () => {
      router.push({ name: "MyAddressList" });
    };
    const userInfo = useUserEffect();
    return { handleLogout, handleAddressClick, userInfo };
  },
};
</script>

<style lang="scss" scoped>
@import "./src/style/viriables.scss";
.wrapper {
  overflow-y: auto;
  position: absolute;
  left: 0;
  top: 0;
  bottom: 0.5rem;
  right: 0;
  background: rgb(248, 248, 248);
}
.header {
  position: relative;
  height: 1.5rem;
  background-image: linear-gradient(
    0deg,
    rgba(0, 145, 255, 0) 4%,
    #0091ff 100%
  );
  padding-top: 0.4rem;
  &__info {
    margin: 0.3rem 0.18rem 0.18rem;
    padding-top: 0.46rem;
    height: 0.66rem;
    background: $bgColor;
    border-radius: 0.08rem;
    &__user {
      font-size: 0.24rem;
      color: #262628;
      text-align: center;
      line-height: 0.36rem;
    }
    &__id {
      margin-top: 0.04rem;
      font-size: 0.14rem;
      color: #c1c0c9;
      text-align: center;
    }
  }
  &__avatar {
    position: absolute;
    left: 50%;
    top: 0.2rem;
    transform: translateX(-50%);
    width: 0.94rem;
    height: 0.94rem;
    border-radius: 50%;
  }
}
.list {
  margin: 0.16rem 0.18rem 0 0.18rem;
  background: $bgColor;
  box-shadow: 0 0.08rem 0.16rem 0 rgba(0, 0, 0, 0.08);
  border-radius: 0.08rem;
  &__item {
    display: flex;
    padding: 0.2rem;
    line-height: 0.22rem;
    font-size: 0.14rem;
    color: #262626;
    &__icon {
      margin-right: 0.12rem;
      width: 0.22rem;
      height: 0.22rem;
      background: #32c5ff;
      border-radius: 0.08rem;
      text-align: center;
      font-weight: bold;
      color: $bgColor;
    }
    &__test {
      flex: 1;
    }
    &__arrow {
      width: 0.2rem;
      transform: rotate(180deg);
      font-weight: bold;
      color: #c2c4ca;
    }
  }
}
.logout {
  margin: 0.18rem;
  line-height: 0.32rem;
  background: $btn-bgColor;
  color: $bgColor;
  text-align: center;
  border-radius: 0.04rem;
}
</style>